<?php
$referer = parse_url($_SERVER['HTTP_REFERER']);
if( $referer['host'] == $GLOBALS['HTTP_HOST'] ){
  switch ($_POST['action']) {
    case 'FindStore' :
      global $DBobject, $SMARTY;
      $data = array ();
      $places = array ();
      $isSuburb = false;
      $coordenates = '';
      $limit = empty($_POST['limit'])?10:$_POST['limit'];
      if (! empty ( $_POST['q'] )) {
      	
      		$sql = "SELECT postcode_lat, postcode_lon FROM tbl_postcode WHERE postcode_lat <> 0 AND postcode_lon <> 0 AND postcode_deleted IS NULL AND CONCAT(postcode_suburb,', ',postcode_state,', ',postcode_postcode) LIKE :query";
      		$places = $DBobject->wrappedSql ( $sql, array (
      				':query' => "%{$_POST['q']}%"
      		) );
      		
      	
      } elseif(!empty($_POST['lat']) && !empty($_POST['lon'])){
      	$coordinates = $_POST['lat'] .', '. $_POST['lon'];
        $places = array (
            array (
                'postcode_lat' => $_POST['lat'],
                'postcode_lon' => $_POST['lon'] 
            ) 
        );
      }
      
      $where = "";
     
      if (! empty ( $places )) {
        $data = GetLocationOrder ( $places, $where, $limit );
      } else {
      	$params= array();
      	$sql = "SELECT * FROM tbl_location WHERE location_latitude <> 0 and location_longitude <> 0 and location_deleted IS NULL AND location_listing_id = 9 AND location_published = 1 {$where}";
        $data = $DBobject->wrappedSql($sql,$params);
      }
      $SMARTY->assign ( 'locations', $data );
      $SMARTY->assign ( 'q', $_POST['q'] );
      $SMARTY->assign ( 'coordinates',  $coordinates);
      $html = $SMARTY->fetch ( 'location-results.tpl' );
      echo json_encode ( array (
          'html' => $html,
      		'data' =>$places
      ) );
      exit ();
  }
}
die ('Error');
